//
//  MyToolView.swift
//  UNHimalayas
//
//  Created by 王水平 on 2019/11/22.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class MyToolView: UIView {

    //声明闭包
    typealias MyToolViewBtnClicked = (_ index : NSInteger?) -> ()
    //定义个变量 类型就是上面声明的闭包
    var viewClicked : MyToolViewBtnClicked?
    
    let arr = [["title" : "扫一扫","imgName" : "main_icon_myspac_record"],
               ["title" : "定时关闭","imgName" : "main_icon_myspac_timer_close"],
               ["title" : "未成年模式","imgName" : "main_icon_myspac_child_close"],
               ["title" : "驾驶模式","imgName" : "main_abc_ic_drive_myspace"]]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initTitleLabel()
        initContentItem()
    }
    
    func initTitleLabel() -> Void {
        
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.text = "必备工具"
        label.textColor = UIColor.black
        label.textAlignment = .left
        label.font = UIFont.boldSystemFont(ofSize: 16)
        self.addSubview(label)
        
        label.snp.makeConstraints { (make) in
            
            make.top.equalToSuperview().offset(kRealWidth(30))
            make.left.equalToSuperview().offset(kRealWidth(20))
            make.width.equalTo(kRealWidth(200))
            make.height.equalTo(kRealWidth(16))
        }
    }
    
    func initContentItem() -> Void {
        
        for i in 0..<4 {
            
            let dict : Dictionary = arr[i]
            
            let itemView = MyItemView()
            itemView.backgroundColor = UIColor.clear
            itemView.imgView.backgroundColor = UIColor.clear
            itemView.imgView.image = UIImage(named: dict["imgName"]!)
            itemView.titleLabel.text = dict["title"]
            itemView.tag = 400 + i;
            itemView.isUserInteractionEnabled = true
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGestureAction))
            tapGesture.numberOfTapsRequired = 1
            itemView.addGestureRecognizer(tapGesture)
            self.addSubview(itemView)
            
            itemView.snp.makeConstraints { (make) in
                
                make.top.equalToSuperview().offset(kRealWidth(76) + kRealWidth(CGFloat((i/4) * 80)))
                make.left.equalToSuperview().offset((kScreenWidth - kRealWidth(30)) / 4 * CGFloat(i%4))
                make.width.equalTo((kScreenWidth - kRealWidth(30)) / 4)
                make.height.equalTo(kRealWidth(50))
                
                if i == 3 {
                    
                    make.bottom.equalToSuperview().offset(kRealWidth(-20))
                }
            }
        }
    }
    
    //MARK: 点击事件
    @objc fileprivate func tapGestureAction(sender: UITapGestureRecognizer){
        
        viewClicked!(sender.view!.tag - 400)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
